<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends CI_Controller {
	public function index() {
		$this->load->model("version_db");

		$this->load->view('header', array("current" => "index"));
		$this->load->view('index', array("version" => $this->version_db->currentVersion()));
		$this->load->view("footer");
	}

	public function version() {
		$this->load->view('header', array("current" => "version"));

		$this->load->model("version_db");
		$this->load->view("version", array(
			"version" => $this->version_db->currentVersion(),
			"allVersion" => $this->version_db->all()
			));
		$this->load->view("footer");
	}

	public function summary() {
		$this->load->view('header', array("current" => "summary"));
		$this->load->view("footer");
	}

	//Manual Test 1->4
	public function manual_test1() {
		$typeStr = "<http://rdf.freebase.com/ns/type.object.name>";
		$name = $_POST["name"];
		$nameStr = "<http://rdf.freebase.com/ns/'$name'>";

		$t1 = microtime(true);
		$query = $this->db->query("SELECT object_1 FROM content WHERE property = '$typeStr' AND object_2 = '$nameStr';");
		$t2 = microtime(true);

		$this->load->view("manual_test3", array('name' => $name, 'query' => $query, 'delay' => (($t2 - $t1) * 1000)));
	}

	public function manual_test2() {
		$ID = $_POST["ID"];
		$t1 = microtime(true);
		$query = $this->db->query("");
		$t2 = microtime(true);
		$this->load->view("manual_test3", array('ID' => $ID, 'query' => $query, 'delay' => (($t2 - $t1) * 1000)));
	}

	public function manual_test3() {
		$ID = $_POST["ID"];
		$t1 = microtime(true);
		$query = $this->db->query("");
		$t2 = microtime(true);
		$this->load->view("manual_test3", array('ID' => $ID, 'query' => $query, 'delay' => (($t2 - $t1) * 1000)));
	}

	public function manual_test4() {
		$ID = $_POST["ID"];
		$t1 = microtime(true);
		$query = $this->db->query("");
		$t2 = microtime(true);
		$this->load->view("manual_test3", array('ID' => $ID, 'query' => $query, 'delay' => (($t2 - $t1) * 1000)));
	}

	//Auto Test 1->4
	public function auto_test1() {
		$this->load->view("auto_test");
	}

	public function auto_test2() {
		$this->load->view("auto_test");
	}

	public function auto_test3() {
		$this->load->view("auto_test");
	}

	public function auto_test4() {
		//$this->load->view("auto_test");
		while(true) {

		}
	}

	public function add_version() {
		$name = $_POST["name"];
		$description = $_POST["description"];
		$is_current = 0;
		if (isset($_POST["check"]))
			$is_current = 1;

		$this->load->model("version_db");
		$ID = $this->version_db->add($name, $description, $is_current);

		if ($is_current > 0)
			$this->version_db->setCurrent($ID);

		$this->load->helper('url');
		redirect(site_url('/test/version'));
	}

	public function edit_version() {
		$ID = $_POST["ID"];
		$name = $_POST["name"];
		$description = $_POST["description"];

		$is_current = 0;
		if (isset($_POST["check"]))
			$is_current = 1;

		$this->load->model("version_db");
		$this->version_db->modify($ID, $name, $description);

		if ($is_current > 0)
			$this->version_db->setCurrent($ID);

		$this->load->helper('url');
		redirect(site_url('/test/version'));
	}

	public function get_result() {
		$id = $_GET["id"];

		$this->load->model("version_db");
		$version = $this->version_db->getVersion($id);
		$result = array();
		for ($i = 1; $i <= 4; $i++) {
			$res = $this->version_db->getResult($id, $i);
			array_push($result, $res);
		}	
		$this->load->view("test_result", array(
			"version" => $version,
			"result" => $result
			));
	}
}
?>